package com.ruoyi.project.business.askleave.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.askleave.entity.AskLeave;
import com.ruoyi.project.business.askleave.request.ListAskLeaveRequest;
import com.ruoyi.project.business.askleave.request.AddAskLeaveRequest;
import com.ruoyi.project.business.askleave.response.AskLeaveResponse;
import com.ruoyi.project.business.askleave.service.IAskLeaveService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 请假Controller
 * 
 * @author ${author}
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/business/askleave")
public class AskLeaveController extends BaseController {
    @Autowired
    private IAskLeaveService askLeaveService;

    /**
     * 查询请假列表
     */
    @PreAuthorize("@ss.hasPermi('business:askleave:list')")
    @GetMapping("/list")
    public TableDataInfo list(ListAskLeaveRequest request) {
        startPage();
        Long tenantId = getTenantId();
        List<AskLeaveResponse> list = askLeaveService.selectAskLeaveList(request, tenantId);
        return getDataTable(list);
    }

    /**
     * 导出请假列表
     */
    @PreAuthorize("@ss.hasPermi('business:askleave:export')")
    @Log(title = "请假", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ListAskLeaveRequest request) {
        Long tenantId = getTenantId();
        List<AskLeaveResponse> list = askLeaveService.selectAskLeaveList(request, tenantId);
        ExcelUtil<AskLeaveResponse> util = new ExcelUtil<>(AskLeaveResponse.class);
        return util.exportExcel(list, "askleave");
    }

    /**
     * 获取请假详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:askleave:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(askLeaveService.selectAskLeaveById(id));
    }

    /**
     * 新增请假
     */
    @PreAuthorize("@ss.hasPermi('business:askleave:add')")
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddAskLeaveRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(askLeaveService.insertAskLeave(request, operatorId, tenantId));
    }

    /**
     * 修改请假
     */
    @PreAuthorize("@ss.hasPermi('business:askleave:edit')")
    @Log(title = "请假", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AskLeave askLeave) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(askLeaveService.updateAskLeave(askLeave, operatorId, tenantId));
    }

    /**
     * 删除请假
     */
    @PreAuthorize("@ss.hasPermi('business:askleave:remove')")
    @Log(title = "请假", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(askLeaveService.deleteAskLeaveByIds(ids, operatorId, tenantId));
    }
}
